#-*- coding: utf-8 -*-
import traceback

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render, render_to_response, RequestContext
from django.contrib.auth.decorators import login_required
from django.template.response import TemplateResponse
from apiManager import viewer
from apiManager.models import Asset
from eve_static.models import Invtypes



#assets main page
def assets(request):
    return render_to_response('assets.html')

#@login_required
def myaccount(request):
    return render_to_response('account.html')

#@login_required
def assetTree(request):
    """ If users are authenticated, direct them to assetTee page. Otherwise, take
    them to the login page.
    """
    return render_to_response("assetTree.html", 
                              {'nodes': Asset.objects.all(), 'items': Asset.objects.filter(parentID = None)},
                              context_instance=RequestContext(request))

def corpItems(request):
    
    return render_to_response("corpItems.html", 
                              {'nodes': Asset.objects.all()},
                              context_instance=RequestContext(request))

def assetlist(request):
    try:
        assetList = Asset.objects.all()
        assetCount = assetList.count()
    except Exception, e:
        print traceback.format_exc()
    
#     assetDict = {}
    if assetCount < 1:
        test = "No assets"
    else:
        test = "Got assets"
        #assetDict = viewer.asset_details(assetList)
        primary_asset = viewer.asset_details(assetList)
#         for asset in assetList:
#             assetAttributes = {}
#             assetType = Invtypes.objects.get(typeid = asset.typeID)
#             assetAttributes['typeID']= assetType.typeid
#             assetAttributes['typeName'] = assetType.typename
#             assetDict[asset.assetID] = assetAttributes
#             assetName = viewer.asset_details(asset)
#             
#             assetName = assetType.typename
        
    return TemplateResponse(request, 'assetlist.html', locals())